﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;

namespace WebserviceLab2
{
    public class Product
    {
        #region "Attributes & Properties"
        private String type;
        private String name;
        private int price;
        private String img;
        private string url;

        public string Url
        {
            get { return url; }
            set { url = value; }
        }
        

        public string Type
        {
            get { return type; }
            set { type = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int Price
        {
            get { return price; }
            set { price = value; }
        }

        public string Img
        {
            get { return img; }
            set { img = value; }
        }
        #endregion

        #region "Constructors"
        public Product()
        {
            this.type = "";
            this.name = "";
            this.price = 0;
            this.img = "";
        }

        public Product(string type, string name, int price, string img)
        {
            this.type = type;
            this.name = name;
            this.price = price;
            this.img = img;
        }

        public Product(Product p)
        {
            this.type = p.Type;
            this.name = p.Name;
            this.price = p.Price;
            this.img = p.Img;
        }
        #endregion

        #region "Methods"
        /// <summary>
        /// Load data from XML file
        /// </summary>
        /// <returns></returns>
        public static XmlDocument loadXML()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/Product.xml"));
            return doc;
        }

        /// <summary>
        /// Get all product
        /// </summary>
        /// <returns></returns>
        public static List<Product> getAll()
        {
            //XDocument xdoc = XDocument.Load("~\\App_Data\\Product.xml");
            XmlDocument doc = loadXML();
            XmlNode root = doc.DocumentElement;
            List<Product> li = new List<Product>();
            if (root != null)
            {
                int len = root.ChildNodes.Count;
                for (int i = 0; i < len; i++)
                {
                    if (root.ChildNodes[i].Attributes == null)
                        continue;
                    Product temp = new Product();
                    temp.Type = root.ChildNodes[i].Attributes[0].Value;
                    temp.Name = root.ChildNodes[i].Attributes[1].Value;
                    temp.Price = Int32.Parse(root.ChildNodes[i].Attributes[2].Value);
                    temp.Img = root.ChildNodes[i].Attributes[3].Value;
                    li.Add(temp);
                }
            }
            return li;
        }

        /// <summary>
        /// Get all product of a type.
        /// </summary>
        /// <param name="type">Type name</param>
        /// <returns></returns>
        public static Product[] getAllByType(string type, string url)
        {
            //XDocument xdoc = XDocument.Load("~\\App_Data\\Product.xml");
            XmlDocument doc = loadXML();
            XmlNode root = doc.DocumentElement;
            List<Product> li = new List<Product>();
            if (root != null)
            {
                int len = root.ChildNodes.Count;
                for (int i = 0; i < len; i++)
                {
                    if (root.ChildNodes[i].Attributes == null)
                        continue;
                    if (root.ChildNodes[i].Attributes[0].Value != type)
                        continue;
                    Product temp = new Product();
                    temp.Type = root.ChildNodes[i].Attributes[0].Value;
                    temp.Name = root.ChildNodes[i].Attributes[1].Value;
                    temp.Price = Int32.Parse(root.ChildNodes[i].Attributes[2].Value);
                    temp.Img = url + "/"+root.ChildNodes[i].Attributes[3].Value;
                    li.Add(temp);
                }
            }
            return li.ToArray();
        }

        public static String[] getType()
        {
            XmlDocument doc = loadXML();
            XmlNode root = doc.DocumentElement;
            List<String> li = new List<String>();
            if (root != null)
            {
                int len = root.ChildNodes.Count;
                for (int i = 0; i < len; i++)
                {
                    if (root.ChildNodes[i].Attributes == null)
                        continue;
                    if (li.Contains(root.ChildNodes[i].Attributes[0].Value))
                        continue;
                    li.Add(root.ChildNodes[i].Attributes[0].Value);
                }
            }
            return li.ToArray();
        }
        #endregion
    }
}